<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\AdminUserFilter;
use App\Admin\Requests\AdminUserProfileRequest;
use App\Admin\Requests\AdminUserRequest;
use App\Admin\Resources\AdminUserResource;
use App\Admin\Models\AdminPermission;
use App\Admin\Models\AdminRole;
use App\Admin\Models\AdminUser;
use App\Admin\Models\VueRouter;
use App\Admin\Models\VueAdminUserRouter;
use App\Admin\Utils\Admin;

class AdminUserController extends Controller
{
    public function user()
    {
        $user = Admin::user();
        if ($user) {
            return $this->ok(
                AdminUserResource::make($user)
                ->for(AdminUserResource::FOR_INFO)
            );
        } else {
            return $this->noAuth("登录已失效，请重新登录");
        }
    }

    public function editUser()
    {
        $user = Admin::user();
        return $this->ok(
            AdminUserResource::make($user)
            ->for(AdminUserResource::FOR_EDIT_INFO)
        );
    }

    public function updateUser(AdminUserProfileRequest $request)
    {
        $inputs = $request->validated();
        Admin::user()->updateUser($inputs);
        return $this->created(
            AdminUserResource::make(Admin::user())
        );
    }

    public function index(
        AdminUserFilter $filter,
    ){
        $adminUsers = AdminUser::withTrashed(isRestore())
            ->filter($filter)
            ->paginate();

        return $this->ok(
            AdminUserResource::forCollection(
                AdminUserResource::FOR_INDEX, $adminUsers
            )->additional($this->formData())
        );
    }

    public function store(
        AdminUserRequest $request, 
        AdminUser $user,
        VueRouter $vueRouter,
        VueAdminUserRouter $vueAdminUserRouter
    ){
        $inputs = $request->validated();
        $user = $user::createUser($inputs);

        if (!empty($q = $request->post('roles', []))) {
            $user->roles()->attach($q);
        }
        if (!empty($q = $request->post('permissions', []))) {
            $user->permissions()->attach($q);
        }

        if (Admin::isAdministrator()) {
            $vueAdminUserRouter->createVueAdminUserRouter(
                $vueRouter->treeWithAuth()->toTree(), 
                $user->id
            );
        }

        return $this->created();
    }

    public function show(Int $id)
    {
        $adminUser = AdminUser::withTrashed(isRestore())->find($id);
        return $this->ok(
            AdminUserResource::make($adminUser)
            ->for(AdminUserResource::FOR_SHOW)
        );
    }

    public function update(
        AdminUserRequest $request, 
        AdminUser $adminUser,
        VueRouter $vueRouter,
        VueAdminUserRouter $vueAdminUserRouter
    ){
        $inputs = $request->validated();
        $adminUser->updateUser($inputs);
        if (isset($inputs['roles'])) {
            $adminUser->roles()->sync($inputs['roles']);
        }
        if (isset($inputs['permissions'])) {
            $adminUser->permissions()->sync($inputs['permissions']);
        }
        if (Admin::isAdministrator()) {
            $vueAdminUserRouter->createVueAdminUserRouter(
                $vueRouter->treeWithAuth()->toTree(), 
                $adminUser->id
            );
        }
        return $this->created(
            AdminUserResource::make($adminUser)
            ->for(AdminUserResource::FOR_EDIT)
        );
    }

    public function destroy(AdminUser $adminUser)
    {
        $adminUser->delete();
        return $this->noContent();
    }

    public function edit(
        AdminUser $adminUser
    ){
        return $this->ok(
            AdminUserResource::make($adminUser)
            ->for(AdminUserResource::FOR_EDIT)
            ->additional($this->formData())
        );
    }

    /**
     * 返回创建和编辑表单所需的选项数据
     *
     * @return array
     */
    protected function formData()
    {
        $roles = AdminRole::query()
            ->orderByDesc('id')
            ->get();
        $permissions = AdminPermission::query()
            ->select(['id', 'name'])
            ->orderByDesc('id')
            ->get();

        return compact('roles', 'permissions');
    }

    public function create()
    {
        $slugs = AdminPermission::$basicPermission;
        $result['data']['permissions'] = AdminPermission::whereIn('slug',$slugs)
            ->select(['id', 'name'])
            ->orderByDesc('id')
            ->get();
        return $this->ok(
            $result+$this->formData()
        );
    }
}
